﻿using MyCompanyName.AbpZeroTemplate.Prm.Project;
using MyCompanyName.AbpZeroTemplate.Prm.Topic.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyCompanyName.AbpZeroTemplate.Prm.Topic
{
    public class TopicAppService : AbpZeroTemplateAppServiceBase, ITopicAppService
    {

        private readonly ProjectManager projectManager;

        public TopicAppService(ProjectManager _projectManager)
        {
            projectManager = _projectManager;
        }
        /// <summary>
        /// 添加项目提问
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task CreateTopic(CreateTopicInPut input)
        {
            await projectManager.topicRepository.InsertAsync(new Project.Topic
            {
                Category = "",
                Content = input.Content,
                ProjectId = input.ProjectId,
                Title = input.Title
            });
        }


        /// <summary>
        /// 获取项目提问
        /// </summary>
        public List<GetTopicOutPut> GetTopic(int projectId)
        {
            List<GetTopicOutPut> result = new List<GetTopicOutPut>();
            var topics = projectManager.topicRepository.GetAll().Where(x => x.ProjectId == projectId).ToList();
            foreach (var item in topics)
            {
                var user = UserManager.Users.Where(x => x.Id == item.CreatorUserId).SingleOrDefault();
                var outPut = new GetTopicOutPut()
                {
                    Id = item.Id,
                    Content = item.Content,
                    Title = item.Title,
                    CreateOn = item.CreationTime.ToString("yyyy-MM-dd"),
                    CreateBy = user == null ? "未知" : user.UserName
                };
                result.Add(outPut);
            }
            return result;
        }


        public List<GetTopicReplyOutPut> GetTopicReply(int topicId)
        {
            List<GetTopicReplyOutPut> result = new List<GetTopicReplyOutPut>();
            var replys = projectManager.replyRepository.GetAll().Where(x => x.TopicId == topicId).ToList();
            foreach (var item in replys)
            {
                var user = UserManager.Users.Where(x => x.Id == item.CreatorUserId).SingleOrDefault();
                var outPut = new GetTopicReplyOutPut()
                {
                    Content = item.Content,
                    CreateBy = user == null ? "未知" : user.UserName,
                    CreateOn = item.CreationTime.ToString("yyyy-MM-dd")
                };
                result.Add(outPut);
            }
            return result;
        }


        public async Task CreateTopicReply(CreateTopicReplyInPut input)
        {
            await projectManager.replyRepository.InsertAsync(new Reply()
            {
                Content = input.Content,
                TopicId = input.TopicId
            });
        }
    }
}
